package com.rfsp.mcp.controller.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.rfsp.common.cache.AreaCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rfsp.common.pojo.vo.AreaVO;
import com.rfsp.mcp.service.PfmService;

@Controller("BaseAreaController")
@RequestMapping("/base/area")
public class BaseAreaController extends BaseController {
	
	public Logger logger = LoggerFactory.getLogger(BaseAreaController.class);
	
	@Autowired
	PfmService pfmService;
	
	@Autowired
	AreaCache areaCache;
	
    @GetMapping("/")
    @ResponseBody
    public List<AreaVO> index1() {
        return index();
    }

    @GetMapping("/index")
    @ResponseBody
    public List<AreaVO> index() {
    	
    	//所有医保平台数据
    	List<String> areaList = pfmService.getAllAreaCode();
    	
    	if(areaList == null || areaList.size() == 0) {
    		return new ArrayList<>();
    	}
    	
    	//缓存里的所有地区名称
    	LinkedHashMap<String, AreaVO> areaMap = areaCache.areaMap;

    	logger.info("areaMap.size="+((areaMap!=null)?areaMap.size():""));

    	Map<String, AreaVO> provinceMap = new HashMap<>();
    	Map<String, AreaVO> cityMap = new HashMap<>();
    	Map<String, AreaVO> countyMap = new HashMap<>();
    	
    	List<AreaVO> provinceList = new ArrayList<>();
    	List<AreaVO> cityList;
    	List<AreaVO> countyList;
    	
    	//循环省 -- 放开就有，注掉就没有
    	for(String area1 : areaList) {
    		String province = area1.substring(0,2) + "0000";
    		
    		AreaVO provinceVO=new AreaVO();
    		provinceVO.setId(province);
    		provinceVO.setParent(province);
    		provinceVO.setValue(province);
    		provinceVO.setText(areaMap.get(province).getText());
			
    		if(!provinceMap.containsKey(province)) {
    			provinceMap.put(province, provinceVO);
    			provinceList.add(provinceVO);
    			
    			//循环市 -- 放开就有，注掉就没有
    			cityList = new ArrayList<>();
    			for(String area2 : areaList) {
    				
    				String city = area2.substring(0,4) + "00";
    				
    				//如果省和市不对应，则跳过
    				if(!area1.substring(0,2).equals(area2.substring(0,2))) {continue;}
    				//如果省==市，则跳过
    				if(province.equals(city)) {continue;}
    				//如果是市辖区，则跳过
    				if("市辖区".equals(areaMap.get(city))) {continue;}
    	    		
    	    		AreaVO cityVO=new AreaVO();
    	    		cityVO.setId(city);
    	    		cityVO.setParent(city);
    	    		cityVO.setValue(city);
    	    		cityVO.setText(areaMap.get(city).getText());

    	    		if(!cityMap.containsKey(city)) {
    	    			cityMap.put(city, cityVO);
    	    			cityList.add(cityVO);
    	    			
    	    			
//    	    			循环区 -- 放开就有，注掉就没有
    	    			countyList = new ArrayList<>();
    	    			for(String area3:areaList) {
    	    				if(!area2.substring(0,4).equals(area3.substring(0,4)) || area1.equals(area2) || area2.equals(area3)) {continue;}
    	    				String county = area3;

    	    	    		AreaVO countyVO=new AreaVO();
    	    	    		countyVO.setId(county);
    	    	    		countyVO.setParent(county);
    	    	    		countyVO.setValue(county);
    	    	    		countyVO.setText(areaMap.get(county).getText());

    	    	    		if(!countyMap.containsKey(county)) {
    	    					countyMap.put(county, countyVO);
    	    	    			countyList.add(countyVO);
    	    	    		}
    	    			}
    	    			if(!countyList.isEmpty()) {
    	    				cityVO.setChildren(countyList);
    	    			}
    	    			
    	    		}
    			}
    			if(!cityList.isEmpty()) {
    				provinceVO.setChildren(cityList);
    			}
    		}
    	}
    	
        return provinceList;
    }
}
